<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>webrx</title>
    <style>
        .page a, .page span {
            display: inline-block;
            text-decoration: none;
            padding: 2px;
            border: 1px solid gray;
            min-width: 16px;
            text-align: center;
            margin: 2px;
            border-radius: 5px;
        }

        .page a:hover {
            color: #fff;
            background-color: #94acef;
        }

        .page span {
            color: #fff;
            background-color: #94acef;
        }
    </style>
</head>
<body>

</body>
</html>

<?php
error_reporting(0);
$conn = new pdo('mysql:host=127.0.0.1;port=3306;dbname=zgdb;charset=utf8', 'root', '', [19 => 2, 1002 => 'set names utf8']);
//分页实现
//(1)求出总记录数
$pst = $conn->prepare('select count(*) from t_user');
$pst->execute();
$result = $pst->fetchAll(PDO::FETCH_NUM);
$recordcount = $result[0][0];
$pst->closeCursor();

//(2)设置每页多少，第几页，计算总页数
$pagesize = 3;
$currpage = isset($_GET['p']) ? $_GET['p'] : 1;
$pagecount = ceil($recordcount / $pagesize);

if ($currpage < 1) $currpage = 1;
if ($currpage >= $pagecount) $currpage = $pagecount;
//(3)查询指定页
$st = $conn->prepare('select * from t_user limit :start,:size');
$start = $currpage * $pagesize - $pagesize;
$size = $pagesize;
$st->bindParam('start', $start, PDO::PARAM_INT);
$st->bindParam('size', $size, PDO::PARAM_INT);
$st->execute();
$rs = $st->fetchAll();
$st->closeCursor();
echo '<pre>';
print_r($rs);

echo '<div class="page">';
if ($currpage > 1) printf("<a href='?p=%d'>上一页</a>", $currpage - 1);
$ss = 1;
$ee = 10;
if ($currpage >= 7) {
    $ss = $currpage - 5;
    $ee = $currpage + 4;
}
foreach (range($ss, $ee) as $value) {
    if ($value > $pagecount) break;
    if ($currpage == $value) {
        printf("<span >%d</span>", $value);
    } else {
        printf("<a href='?p=%d'>%d</a>", $value, $value);
    }
}

if ($currpage < $pagecount) printf("<a href='?p=%d'>下一页</a>", $currpage + 1);
echo '</div>';
?>